package day_2023_7_to_10;

/**
 * @author haomin
 * @date 2023/09/07 23:58
 **/
public class Day_9_7 {
    class Solution {
        public long repairCars(int[] ranks, int cars) {
            long l = 1, r = 1l * ranks[0] * cars * cars;
            while (l < r) {
                long m = l + r >> 1;
                if (check(ranks, cars, m)) {
                    r = m;
                } else {
                    l = m + 1;
                }
            }
            return l;
        }

        public boolean check(int[] ranks, int cars, long m) {
            long cnt = 0;
            for (int x : ranks) {
                cnt += (long) Math.sqrt(m / x);
            }
            return cnt >= cars;
        }
    }
}